<template>
	<view class="my_ticket">
		<Navbar>票号</Navbar>
		<scroll-view scroll-y class="scroll_view" @scrolltolower="loadmore">
			<view class="item" v-for="item in list">
				<view class="top">
					<view class="type">
						<image :src="`${baseImageUrl}/common/gy.png`" v-if="item.field=='silver'"></image>
						<image :src="`${baseImageUrl}/common/hot_cover1.png`" v-else-if="item.field=='foodstuff'">
						</image>
						<image :src="`${baseImageUrl}/common/hot_cover2.png`" v-else-if="item.field=='cloth'"></image>
						<image :src="`${baseImageUrl}/common/hot_cover3.png`" v-else-if="item.field=='titledeed'">
						</image>
						<image :src="`${baseImageUrl}/common/hot_cover4.png`" v-else-if="item.field=='music'"></image>
						<image :src="`${baseImageUrl}/common/hot_cover5.png`" v-else></image>
						<text>{{item.ticket.name}}</text>
					</view>
					<view class="btn" @click="submit(item)" v-if="item.status==0">取出</view>
				</view>
				<view class="time">
					<view class="key">买入</view>
					<view class="value">{{item.created_at}}</view>
				</view>
				<view class="info">
					<!-- <view>
						<view>买入价格</view>
						<view>{{item.price}}</view>
					</view> -->
					<view>
						<view>买入数量</view>
						<view>{{item.number}}</view>
					</view>
					<view>
						<view>当前盈亏</view>
						<view>{{item.profit}}</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="loadmore_status" :color="$store.state.loadmoreColor"
				:lineColor="$store.state.loadmoreColor" line @loadmore="loadmore" />
		</scroll-view>
		<u-popup :show="popup_show" bgColor="transparent" mode="center" @close="close">
			<TakeOut :field="field" @close="close" @confirm="confirm" v-if="popup==1" />
			<TakeSuccess @close="close" v-if="popup==2" />
			<TakeFail @close="close" v-if="popup==3" />
		</u-popup>
	</view>
</template>

<script>
	// import {
	// 	captchaAppId
	// } from '@/utils/config.js';
	import {
		ticketMy,
		ticketTake
	} from '@/request/api.js';
	import TakeOut from '@/components/home/TakeOut.vue';
	import TakeSuccess from '@/components/home/TakeSuccess.vue';
	import TakeFail from '@/components/home/TakeFail.vue';

	export default {
		components: {
			TakeOut,
			TakeSuccess,
			TakeFail
		},
		data() {
			return {
				list: [],

				id: null,
				field: "",
				popup_show: false,
				popup: 1
			}
		},
		onLoad() {
			this.getList();
		},
		methods: {
			async getList() {
				let {
					list
				} = await ticketMy({
					page: this.page,
					limit: this.limit
				});
				this.last_page = list.last_page;
				list = list.list;
				this.list = this.page == 1 ? list : [...this.list, ...list];
				this.loadmore_status = this.last_page == this.page ? 'nomore' : 'loadmore';
			},
			loadmore() {
				if (this.page < this.last_page) {
					this.page++;
					this.getList();
				}
			},
			submit(item) {
				this.field = item.field;
				this.id = item.id;
				this.open(1)
			},
			open(index) {
				this.popup = index;
				this.popup_show = true;
			},
			close() {
				this.popup_show = false;
			},
			async confirm() {
				// const captcha = new TencentCaptcha(captchaAppId, async (data) => {
				// 	if (data.ret !== 0) return false;
				// 	let {
				// 		randstr,
				// 		ticket
				// 	} = data;
				await ticketTake({
					id: this.id
				}, {
					custom: {
						catch: true,
						toast: false
					}
				}).then(res => {
					this.popup = 2;
					this.page = 1;
					this.list = [];
					this.getList();
				}).catch(err => {
					this.popup = 3;
				});
				// }, {});
				// captcha.show();
			}
		}
	}
</script>

<style scoped lang="scss">
	.my_ticket {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});
			padding: 20rpx 10rpx 0;
			box-sizing: border-box;

			.item {
				width: 100%;
				height: 290rpx;
				margin-top: 20rpx;
				padding: 30rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/common/item_730_290.png) no-repeat;
				background-size: 100% 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.top {
					height: 80rpx;
					display: flex;
					justify-content: space-between;

					.type {
						display: flex;
						align-items: center;

						image {
							width: 60rpx;
							height: 60rpx;
						}

						text {
							color: #494338;
							font-size: 32rpx;
							font-weight: 400;
							font-family: SimSun;
							margin-left: 12rpx;
						}
					}

					.btn {
						width: 150rpx;
						height: 80rpx;
						line-height: 80rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 30rpx;
						font-weight: 400;
						font-family: SimSun;
						background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
						background-size: 100% 100%;
					}
				}

				.time {
					margin-bottom: 30rpx;
					display: flex;

					.key {
						width: 76rpx;
						height: 32rpx;
						line-height: 32rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 26rpx;
						font-weight: 400;
						font-family: SimSun;
						background: url(#{$baseImageUrl}/common/btn5.png) no-repeat;
						background-size: 100% 100%;
					}

					.value {
						color: #494338;
						font-size: 26rpx;
						font-weight: 400;
						font-family: SimSun;
						padding-left: 16rpx;
						box-sizing: border-box;
					}
				}

				.info {
					display: flex;
					justify-content: space-between;

					.key {
						color: #494338;
						font-size: 24rpx;
						font-weight: 400;
						font-family: SimSun;
					}

					.value {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin-top: 10rpx;
					}
				}
			}
		}
	}
</style>